﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BookClubIt.DTO;
using System.Data.SqlClient;
using BookClubIt.Data.Commands;
using System.Data;
using System.Web;
using System.Net;
using System.Net.Mail;


namespace BookClubIt.Data
{
    public class EmailData
    {

        public void SendWelcomeEmail(int uid)
        {
            SqlCommand command = EmailCommandList.GetRegisteredEmail;
            BciDatabase db = new BciDatabase("Bookclub");

            command.Parameters["@User_Id"].Value = uid;

            using (SqlDataReader dr = db.ExecuteReader(command))
            {
                Member member = new Member();

                if (dr != null && !dr.IsClosed && dr.Read())
                {
                    while (dr.Read())
                    {
                        string emailAddress = (string)dr["Email"];
                        MailMessage mail = new MailMessage();
                        mail.Subject = "Welcome to bookclubit";
                        mail.From = new MailAddress("noreply@bookclubit.com", "bookclubit");
                        mail.To.Add(new MailAddress(emailAddress.ToString()));
                        mail.Body = @"<html>
                                        <head>
                                        </head>
                                        <body>
                                        <table width='600px' cellpadding='5' cellspacing='5' border='1px' bordercolor='#0088CC'>
                                        <tr><td bgcolor='#0088CC' style='padding:5px;'><img src='http://www.petafy.com/images/logo.png'/></td></tr>
                                        <tr><td style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Hello,</p>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Thank you for joing <A href='http://www.bookclubit.com'>bookclubit</A>! </p>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>You are well on your way to create your very own online book club! <a href='http://www.bookclubit.com/startbookclub.aspx'>Click here</a> to get started.</p>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Thanks,</p>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Bookclubit.com</p>
                                        </td></tr>
                                        </table>
                                        </body>
                                        </html>
                                        ";
                        mail.IsBodyHtml = true;

                        //create instance of smtpclient
                        SmtpClient smtp = new SmtpClient();
                        smtp.Port = 2525;
                        smtp.UseDefaultCredentials = false;
                        smtp.Credentials = new NetworkCredential("noreply@bookclubit.com", "password1");
                        smtp.Host = "mail.bookclubit.com";
                        smtp.EnableSsl = false;
                        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                        try
                        {
                            smtp.Send(mail);
                           
                        }
                        catch (SmtpFailedRecipientException ex)
                        {
                            ex.GetBaseException();
                        }
                    

                     
                    }
                }
            }
        }
        public void CommentPostedEmail(int cid, int tid, int uid, string comment, string comment_user)
        {
            
            SqlCommand command = EmailCommandList.GetBookClubEmails;
            BciDatabase db = new BciDatabase("Bookclub");

            command.Parameters["@Bookclub_Id"].Value = cid;
            command.Parameters["@Topic_Id"].Value = tid;
            command.Parameters["@User_Id"].Value = uid;
           
            using (SqlDataReader dr = db.ExecuteReader(command))
            {
                if (dr != null && !dr.IsClosed && dr.Read())
                {
                    while (dr.Read())
                    {
                        string emailAddress = (string)dr["Email"];

                        MailMessage mail = new MailMessage();

                        mail.Subject = "Someone added a comment to your book club";

                        mail.Body = string.Format(@"<html>
                                        <head>
                                        </head>
                                        <body>
                                        <table width='600px' cellpadding='0' cellspacing='0' border='1px' bordercolor='#0088CC'>
                                        <tr><td bgcolor='#0088CC' style='padding:10px;'><img src='http://www.bookclubit.com/images/email-logo.png'/></td></tr>
                                        <tr><td style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>
                                        <table width='100%' cellpadding='5' cellspacing='1' bgcolor='#0088CC'>
                                        <tr><td style='padding:10px;' bgcolor='#ffffff'>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Hello,</p>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>{0} added a comment to your book club at <a href='http://www.bookclubit.com'>bookclubit.com</a>!</p>
                                        </td>
                                        </tr>
                                        <tr>
                                        <td bgcolor='#eeeeee' style='padding:10px;'>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;padding:5px;backround:#eeeeee'>They said: {1}</p>
                                        </td>
                                        </tr>
                                        <tr>
                                        <td style='padding:10px;' bgcolor='#ffffff'>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>To reply to the comment, simply <a href='http://www.bookclubit.com/bookclub/topic.aspx?id={2}&tid={3}'>click here</a> and keep the discussion going.</p>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Thanks,</p>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Bookclubit.com</p>
                                        </td>
                                        </tr>
                                        </table>
                                        </td></tr>
                                        </table>
                                        </body>
                                        </html>", comment_user, comment, cid, tid);
                        mail.IsBodyHtml = true;
                        mail.From = new MailAddress("noreply@bookclubit.com", "bookclubit");
                        mail.To.Add(new MailAddress(emailAddress.ToString()));

                        //create instance of smtpclient
                        SmtpClient smtp = new SmtpClient();
                        smtp.Port = 2525;
                        smtp.UseDefaultCredentials = false;
                        smtp.Credentials = new NetworkCredential("noreply@bookclubit.com", "password1");
                        smtp.Host = "mail.bookclubit.com";
                        smtp.EnableSsl = false;
                        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                        try
                        {
                            smtp.Send(mail);
                           
                        }
                        catch (SmtpFailedRecipientException ex)
                        {
                          ex.GetBaseException();
                        }


                    }
                }
            }
        }
        public void SomeoneJoinedClubEmail(int cid, int leader_uid, bool isPrivate)
        {

            SqlCommand command = EmailCommandList.GetBookClubAdminEmails;
            BciDatabase db = new BciDatabase("Bookclub");

            command.Parameters["@Bookclub_Id"].Value = cid;
            command.Parameters["@User_Id"].Value = leader_uid;

            using (SqlDataReader dr = db.ExecuteReader(command))
            {
                if (dr != null && !dr.IsClosed && dr.Read())
                {
                    while (dr.Read())
                    {
                        string emailAddress = (string)dr["Email"];

                        MailMessage mail = new MailMessage();

                        if (isPrivate == false)
                        {
                            mail.Subject = "Someone joined your book club";
                            mail.Body = string.Format(@"<html>
                                        <head>
                                        </head>
                                        <body>
                                        <table width='600px' cellpadding='0' cellspacing='0' border='1px' bordercolor='#0088CC'>
                                        <tr><td bgcolor='#0088CC' style='padding:10px;'><img src='http://www.bookclubit.com/images/email-logo.png'/></td></tr>
                                        <tr><td style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>
                                        <table width='100%' cellpadding='5' cellspacing='1' bgcolor='#0088CC'>
                                        <tr><td style='padding:10px;' bgcolor='#ffffff'>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Hello,</p>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Someone your book club at <a href='http://www.bookclubit.com'>bookclubit.com</a>!</p>
                                        </td>
                                        </tr>
                                        <tr>
                                        <td style='padding:10px;' bgcolor='#ffffff'>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Someone joined your book club! <a href='http://www.bookclubit.com/bookclub/members.aspx?id='>Click here</a> to see who.</p>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Thanks,</p>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Bookclubit.com</p>
                                        </td>
                                        </tr>
                                        </table>
                                        </td></tr>
                                        </table>
                                        </body>
                                        </html>");
                        }
                        else
                        {
                            mail.Subject = "Someone requested to join your book club";
                            mail.Body = string.Format(@"<html>
                                        <head>
                                        </head>
                                        <body>
                                        <table width='600px' cellpadding='0' cellspacing='0' border='1px' bordercolor='#0088CC'>
                                        <tr><td bgcolor='#0088CC' style='padding:10px;'><img src='http://www.bookclubit.com/images/email-logo.png'/></td></tr>
                                        <tr><td style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>
                                        <table width='100%' cellpadding='5' cellspacing='1' bgcolor='#0088CC'>
                                        <tr><td style='padding:10px;' bgcolor='#ffffff'>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Hello,</p>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Someone wants to join your book club at <a href='http://www.bookclubit.com'>bookclubit.com</a>!</p>
                                        </td>
                                        </tr>
                                        <tr>
                                        <td style='padding:10px;' bgcolor='#ffffff'>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Please <a href='http://www.bookclubit.com/bookclub/members.aspx?id='>Click here</a> to approve or deny this member into your private book club.</p>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Thanks,</p>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Bookclubit.com</p>
                                        </td>
                                        </tr>
                                        </table>
                                        </td></tr>
                                        </table>
                                        </body>
                                        </html>");
                        }
                        mail.IsBodyHtml = true;
                        mail.From = new MailAddress("noreply@bookclubit.com", "bookclubit");
                        mail.To.Add(new MailAddress(emailAddress.ToString()));

                        //create instance of smtpclient
                        SmtpClient smtp = new SmtpClient();
                        smtp.Port = 2525;
                        smtp.UseDefaultCredentials = false;
                        smtp.Credentials = new NetworkCredential("noreply@bookclubit.com", "password1");
                        smtp.Host = "mail.bookclubit.com";
                        smtp.EnableSsl = false;
                        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                        try
                        {
                            smtp.Send(mail);

                        }
                        catch (SmtpFailedRecipientException ex)
                        {
                            ex.GetBaseException();
                        }


                    }
                }
            }
        }
    }


}